package com.administration.managedBean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.administration.controller.AdministrationController;
import com.administration.entity.Access;
import com.administration.entity.Category;
import com.administration.entity.Groups;
import com.administration.entity.GroupPrivilege;
import com.administration.entity.resultsetmapping.PrivilegeMapping;
import com.common.Constant;
import com.core.managedBean.BaseBean;

/**
 * @author Karim Sherif
 */
@ManagedBean
@ViewScoped
public class GroupBean extends BaseBean {

	private static final long serialVersionUID = -8177203959001729173L;
	private Groups group;
	List<Groups> groupsList = new ArrayList<Groups>();
	// private GroupPrivilegeBean groupPrivilegeBean = new GroupPrivilegeBean();
	private List<Category> categoryList;

	private List<GroupPrivilege> groupPrivilegeList;
	private List<PrivilegeMapping> privilegeMappingList;
	private List<Access> accessList;

	AdministrationController administrationController;

	@PostConstruct
	public void loadData() {
		if (administrationController == null) {
			administrationController = new AdministrationController();
		}
		loadGroupDetailes();
		loadAllGroups();

	}

	public void loadGroupDetailes() {
		Object groupObject = super.getFlashAttribute("group");
		if (groupObject != null) {
			this.group = (Groups) groupObject;
			// groupPrivilegeBean.setGroup(this.group);
			// groupPrivilegeBean.loadAllList();
			// groupPrivilegeBean.loadGroupPrivileges();

			loadAllList();
			loadGroupPrivileges();
		} else {
			group = new Groups();
			// groupPrivilegeBean.loadAllList();
			loadGroupPrivileges();
		}
	}

	public void loadAllList() {
		try {
			this.accessList = administrationController.getAllAccess();
			this.privilegeMappingList = administrationController
					.getAllPrivilegeMappingList();
			this.groupsList = administrationController.getAllGroups();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void loadGroupPrivileges() {
		if (group != null && group.getRecid() != null) {
			try {
				groupPrivilegeList = administrationController
						.getAllGroupPrivileges(group.getRecid());
				for (PrivilegeMapping pMap : this.privilegeMappingList) {
					if (groupPrivilegeList != null
							&& groupPrivilegeList.size() > 0) {
						for (GroupPrivilege gp : groupPrivilegeList) {
							if (pMap.getPrivilegeId().longValue() == gp
									.getPrivilageId().longValue()) {
								pMap.setAccessId(gp.getAccessId());
								pMap.setRecId(gp.getRecid());
								pMap.setGroupId(group.getRecid() );
								break;
							} else {
								pMap.setAccessId(Constant.NO_ACCESS);
//								pMap.setRecId(gp.getRecid());
								pMap.setGroupId(group.getRecid() );
							}
						}
					} 
//					else {
//						pMap.setAccessId(Constant.NO_ACCESS);
//
//					}
				}
			} catch (Exception e) {
				super.addErrorMessage("Ann Error Occured");
				e.printStackTrace();
			}

		}

	}

	public void getAllCategories() {
		try {
			this.categoryList = administrationController.getAllCategories();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void loadAllGroups() {
		try {
			groupsList = administrationController.getAllGroups();
		} catch (Exception ex) {
			super.addErrorMessage("please check your network/internet connection");
			ex.printStackTrace();
		}

	}

	public void saveGroupWithPrivileges() {
		try {

			Long groupId = administrationController.saveGroupWithPrivileges(
					group, privilegeMappingList);
			this.group.setRecid(groupId);
			super.addInfoMessage("Successfully Saved ");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void deleteGroupWithPrivileges() {
		try {
			administrationController.deleteGroupWithPrivileges(group);
			loadAllGroups();
			super.addInfoMessage("Successfully Deleted Group "
					+ group.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String toCreateGroup() {
		super.setFlashAttribute("group", null);
		super.setFlashAttribute("editMode", false);
		return "toCreateGroup";
	}

	public String toEditGroup() {
		super.setFlashAttribute("group", group);
		return "toEditGroup";
	}

	public void toConfirmDelete() {
		super.setFlashAttribute("group", group);
		// return null;
	}

	public String toGroupIndex() {
		return "toGroupIndex";
	}

	// \\###############################################################################################################
	public Groups getGroup() {
		return group;
	}

	public void setGroup(Groups group) {
		this.group = group;
	}

	public List<Groups> getGroupsList() {
		return groupsList;
	}

	public void setGroupsList(List<Groups> groupsList) {
		this.groupsList = groupsList;
	}

	// public void setGroupPrivilegeBean(GroupPrivilegeBean groupPrivilegeBean)
	// {
	// this.groupPrivilegeBean = groupPrivilegeBean;
	// }
	//
	// public GroupPrivilegeBean getGroupPrivilegeBean() {
	// return groupPrivilegeBean;
	// }

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public List<GroupPrivilege> getGroupPrivilegeList() {
		return groupPrivilegeList;
	}

	public void setGroupPrivilegeList(List<GroupPrivilege> groupPrivilegeList) {
		this.groupPrivilegeList = groupPrivilegeList;
	}

	public List<PrivilegeMapping> getPrivilegeMappingList() {
		return privilegeMappingList;
	}

	public void setPrivilegeMappingList(
			List<PrivilegeMapping> privilegeMappingList) {
		this.privilegeMappingList = privilegeMappingList;
	}

	public List<Access> getAccessList() {
		return accessList;
	}

	public void setAccessList(List<Access> accessList) {
		this.accessList = accessList;
	}

}
